
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:Initialize()

	self.Entity:SetModel( "models/\props_phx/box_amraam.mdl" ) 
	self.Entity:SetName("Wired Amraam Missile Launcher")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.Inputs = Wire_CreateInputs( self.Entity, { "X", "Y", "Z", "Launch", "Lock", "LockTime"} )

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(true)
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
	end

    	self.Entity:SetKeyValue("rendercolor", "255 255 255")
	self.PhysObj = self.Entity:GetPhysicsObject()


end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( "amraam_launcher" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	ent:SetVar("Owner", Ply)
	
	return ent
	
end

function ENT:TriggerInput(iname, value)
	if (iname == "X") then
		self.XCo = value

	elseif (iname == "Y") then
		self.YCo = value

	elseif (iname == "Z") then
		self.ZCo = value

	elseif (iname == "Launch") then
		if (value > 0) then
			if (self.Launched == 0) then
				self.Launched = 1
			end
		elseif (value <= 0) then
			self.Launched = 0
		end

	elseif (iname == "Lock") then
		if (value > 0) then
			self.Locked = true
		else
			self.Locked = false
		end

	elseif (iname == "LockTime") then
		if (value > 0) then
			self.LTime = value
		else
			self.LTime = 1000000
		end
	end
end

function ENT:PhysicsUpdate()

end

function ENT:Think()
	if (self.Launched == 1) then
		local NewRock = ents.Create( "launched_amraam" )
		if ( !NewRock:IsValid() ) then return end
		NewRock:SetPos( self.Entity:GetPos() + (self.Entity:GetForward() * 30) + (self.Entity:GetUp() * 5) )
		NewRock:SetModel( "models/\props_phx/amraam.mdl" )
		NewRock:SetAngles( self.Entity:GetAngles() )
		NewRock.ParL = self.Entity
		NewRock:SetVar("Owner", Ply)
		NewRock:Spawn()
		NewRock:Initialize()
		NewRock:Activate()

		self.Launched = 2
	end
end

function ENT:PhysicsCollide( data, physobj )
	
end

function ENT:OnTakeDamage( dmginfo )
	
end

function ENT:Use( activator, caller )

end